const MathLib = require('MathLib').MathLib;
const Coordinate = require('MathLib').Coordinate;
const CalcWorkSpace = require('MathLib').CalcWorkSpace;
const CanvasPlus = require('CanvasPlus').CanvasPlus;

let Color = {
  bgColor: '#fff',
  positionSignColor: '#000',
  tipSignColor: '#2ba246',
  contentColor: '#000',
  logoBgColor: '#2c95ce',

  contentColorHandler: false, 

  logoBgUri: false,
  logoImageOffset: 0,


  tipBgUri: false,
  tipImageOffset: 0
}

function Builder(canvasId, option){
  this.canvasId = canvasId;
  let canvas = wx.createCanvasContext(canvasId);
  this.cp = new CanvasPlus(canvas);
  for (let index in Color) if (Color.hasOwnProperty(index)) Color[index] = (option[index] || option[index] === false) ? option[index] : Color[index];  
}

/**
 * setColor 设置颜色参数
 * @param option Color 颜色对象
 */
function setColor(option){
  for (let index in Color) if (Color.hasOwnProperty(index)) Color[index] = ((option[index] || option[index] === false) ? option[index] : Color[index]);
  return this;
}
Builder.prototype.setColor = setColor;

/**
 * loadData 载入数据
 * @param code JsonSting json代码
 */
function loadData(code) {
  this.code = (((typeof code) == "object") ? code : JSON.parse(code));
  return this;
};
Builder.prototype.loadData = loadData;

/**
 * fillSign 填充定位标志
 */
function fillSign(){
  let cp = this.cp, logoImageOffset = Color.logoImageOffset, tipImageOffset = Color.tipImageOffset;
  cp.save().beginPath();
  cp.circle(215, 215, 95).fill(Color.logoBgColor);// Logo
  if (Color.logoBgUri){
    cp.circle(215, 215, 95).clip().drawImage(Color.logoBgUri, 215 - 95 + logoImageOffset, 215 - 95 + logoImageOffset, 95 * 2 - logoImageOffset * 2, 95 * 2 - logoImageOffset*2);// Logo
  }
  cp.restore().closePath();

  cp.save().beginPath();
  cp.circle(340, 340, 40).fill(Color.tipSignColor);// Tip
  if (Color.tipBgUri) {
    cp.circle(340, 340, 40).clip().drawImage(Color.tipBgUri, 340 - 40 + tipImageOffset, 340 - 40 + tipImageOffset, 40 * 2 - tipImageOffset * 2, 40 * 2 - tipImageOffset*2);// Logo
  }

  cp.restore().closePath();


  cp.beginPath().circle(90, 90, 18, true, true).circle(90, 90, 12, false, true).closePath().fill(Color.positionSignColor);// Position
  cp.circle(90, 90, 4).fill(Color.positionSignColor);// Position

  cp.beginPath().circle(90, 340, 18, true, true).circle(90, 340, 12, false, true).closePath().fill(Color.positionSignColor);// Position
  cp.circle(90, 340, 4).fill(Color.positionSignColor);// Position

  cp.beginPath().circle(340, 90, 18, true, true).circle(340, 90, 12, false, true).closePath().fill(Color.positionSignColor);// Position
  cp.circle(340, 90, 4).fill(Color.positionSignColor);// Position
};
Builder.prototype.fillSign = fillSign;

/**
 * fillContent 填充内容
 */
function fillContent(painter) {
  let cp = this.cp;
  let middle = [215, 215];
  let line = 106;
  let r2 = 8;
  let code = this.code;
  let rows = [
    13, 13, 13, 13, 6, 13, 13, 13, 13,
    13, 13, 13, 5, 4, 5, 13, 13, 13,
    13, 13, 13, 13, 6, 13, 13, 13, 13,
    13, 13, 13, 13, 6, 13, 13, 13, 13
  ];
  let array = [];
  for (let i = 1; i <= 36; i++) {
    let rad = i * 10 - 5;
    let row = code[i - 1].toString(2);
    for (let t = 0; t < rows[i - 1]; t++) {
      let cws = new CalcWorkSpace();
      cws.append({ rad, line, middle, r2, t });
      cws.x1 = cws.middle[0] + cws.round(cws.sin(cws.deg2rad(cws.rad)) * (cws.line + (cws.t * cws.r2)));
      cws.y1 = cws.middle[1] - cws.round(cws.cos(cws.deg2rad(cws.rad)) * (cws.line + (cws.t * cws.r2)));
      if (row[t] === "1") cp.circle(cws.x1, cws.y1, r2 / 2).fill(painter ? painter(i,t) : Color.contentColor);
    }
  }
};
Builder.prototype.fillContent = fillContent;

/**
 * toImage 转为图片
 */
function toImage(callback) {
  let cp = this.cp, ThenHandler;
  this.cp.rect(0,0,430,430).fill(Color.bgColor);
  this.fillSign();
  this.fillContent(Color.contentColorHandler);
  let canvasToTempFilePathComplete = (res) => { 
    (callback || (() => { }))((res.errMsg === "canvasToTempFilePath:ok") ? res.tempFilePath : false);
    (ThenHandler || (()=>{}) )();
  };
  let drawComplete = () => { wx.canvasToTempFilePath({ width: 430, height: 430, canvasId: this.canvasId, complete: canvasToTempFilePathComplete }) };
  this.cp.draw(false, drawComplete);
};
Builder.prototype.toImage = toImage;


module.exports.WxQrCoder = Builder;
